//存放主要交互逻辑js代码
//javascript  模块化
var seckill = {
		
		URL:{
			
			serverTimeURL:function(){
				return '/seckill/serverTime';
			},
			exposerURL:function(){
				return '/seckill/exposer';
			},
			executeSeckillURL:function(){
				return '/seckill/execution1';
			}
		},
		validPhone:function(phone){
			if(!(/^1[3|5|7|8][0-9]\d{4,8}$/.test(phone))){
				return false;
			}
			return true;
		},
		countdown:function(result,startTime,endTime,seckillId){
			if(result.code==200){
				var seckill_box=$('#seckill-box');
				//如果秒杀还未开始
				if(result.data<startTime){
					seckill_box.countdown(startTime,function(event){
						$(this).html(event.strftime("%-d 天 %-H 小时 %M 分 %S 秒"));
					}).on('finish.countdown',function(){
						if(startTime<=result.data){
							seckill.exposer(seckillId,$(this));
						}
					}).countdown('start');
				}else if(result.data >endTime){
					seckill_box.html('<span>秒杀已经结束了！</span>');
				}else{
					seckill.exposer(seckillId,seckill_box);
				}
				
			}
		},
		exposer:function(seckillId,obj){
			$.post(seckill.URL.exposerURL()+"/"+seckillId,function(res){
				console.log(res);
				if(res.code==200){
					if(res.data.md5){
						obj.hide().html('<button class="btn btn-danger" id="seckill-btn" >开始秒杀</button>').show(300);
						$("#seckill-btn").click(function(){
							seckill.executeSeckill(seckillId,res.data.md5);
						})
					}
				}
			});
		},
		executeSeckill:function(seckillId,md5){
			$.post(seckill.URL.executeSeckillURL()+"/"+seckillId+"/"+md5,function(res){
				$('#seckill-box').hide();
				if(res.code == 200){
					var time = new Date(res.data.successKilled.createTime);
					var dateTime=time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()+" "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
					if(res.data.successKilled.state==0){
						state="待付款";
					}
					$('#table').find('tbody')
							   .html("<tr>"+
				    				"<td>"+res.data.successKilled.seckill.name+"</td>"+
				    				"<td>"+res.data.successKilled.userPhone+"</td>"+
				    				"<td>"+dateTime+"</td>"+
				    				"<td>"+res.data.stateInfo+"</td>"+
				    				"<td>"+state+"</td>"+
				    				"</tr>");
					$('#table').show();
				}else{
					$("#fail-msg").html(res.msg);
				}
			})
		},
		//详情页的秒杀逻辑
		detail:{
			init:function(params){
				var seckillId = params.seckillId;
				var userPhone = $.cookie("userPhone");
				var startTime = params.startTime;
				var endTime = params.endTime;
				
				//验证一下手机号
				if(!seckill.validPhone(userPhone)){
					$("#modal").modal({
						show:true,
						backdrop:'static',
						keyboard:false,
					});
				}
				//提交手机号
				$('#btn-phone').click(function(){
					var phone = $('#userPhone').val();console.log(phone);
					if(!seckill.validPhone(phone)){
						$('#msg').hide().html('<label class="label label-danger ">手机号有误</label>').show(500);
						return false;
					}else{
						$.cookie("userPhone",phone,{expires:7,path:"/seckill"});
						window.location.reload();
					}
				})
				//获取服务器时间,判断按钮或倒计时间显示
				$.get(seckill.URL.serverTimeURL(),function(result){
					seckill.countdown(result,startTime,endTime,seckillId);
				})
				
			},	
		}
};